using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.ComponentModel;
using System.Data.OracleClient;
using WPFExcelReport.Helpers;
using WPFExcelReport.Controls;
namespace WPFExcelReport
{
    public partial class VXRCV
    {
        public DataRow Convert2XRCV(DataRow dwInput, string ITXDBVERSION, bool IsConvert = false)
        {
            DataSet SResult = new DataSet();
            SResult = QueryXRCV(dwInput, 0);
            DataRow dwReturn = null;
            if (SResult.Tables[0].Rows.Count > 0)
            {
                dwReturn = SResult.Tables[0].Rows[0];
            }
            else
            {
                dwReturn = SResult.Tables[0].NewRow();
            }
            switch (ITXDBVERSION)
            {
                case "YGR":
                    #region=========
         dwReturn["KBXRMANDT"] = " ".PadRight(3 ); 
         dwReturn["KBXRWERKS"] = " ".PadRight(4 ); 
         dwReturn["KBXRTGINR"] = " ".PadRight(4 ); 
         dwReturn["KBXRTGMON"] = " ".PadRight(2 ); 
         dwReturn["KBXRTGDAY"] = " ".PadRight(2 ); 
         dwReturn["KBXRTGLNR"] = " ".PadRight(0 ); 
         dwReturn["KBXRQUITT"] = " ".PadRight(1 ); 
         dwReturn["KBXRPRTFL"] = " ".PadRight(1 ); 
         dwReturn["KBXRTGTIM"] = " ".PadRight(4 ); 
         dwReturn["KBXRSNDRP"] = " ".PadRight(2 ); 
         dwReturn["KBXRTNONR"] = " ".PadRight(1 ); 
         dwReturn["KBXRSWERK"] = " ".PadRight(4 ); 
         dwReturn["KBXRSNONR"] = " ".PadRight(1 ); 
         dwReturn["KBXRTGFREE"] = " ".PadRight(8 ); 
         dwReturn["KBXRTNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBXRSNNAM"] = " ".PadRight(6 ); 
         dwReturn["KBXRTAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBXRSAPPL"] = " ".PadRight(5 ); 
         dwReturn["KBXRDAT01"] = " ".PadRight(1000 ); 
         dwReturn["KBXRDAT02"] = " ".PadRight(1000 ); 
         dwReturn["KBXRSTATU"] = " ".PadRight(2 ); 
         dwReturn["KBXRQWERK"] = " ".PadRight(4 ); 
         dwReturn["KBXRQNONR"] = " ".PadRight(1 ); 
         dwReturn["KBXRQCONN"] = " ".PadRight(2 ); 
         dwReturn["KBXRSTAMP"] = " ".PadRight(14 ); 
                    #endregion
                    break;
                case "JLT":

                    break;
                default:

                    break;
            }
            if (IsConvert)
            {
                foreach (DataColumn cm in dwInput.Table.Columns)
                {
                    if (dwInput[cm.ColumnName].ToString() == "")
                        continue;
                    if (dwReturn.Table.Columns.Contains(cm.ColumnName))
                    {
                        dwReturn[cm.ColumnName] = dwInput[cm.ColumnName];
                    }
                }
            }
            return dwReturn;
        }
        public void MapXRCV(ref DataRow dwLocal, DataRow dwData)
        {
            BlindMap(ref dwLocal, dwData);
        }
        public void SaveXRCV(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            DataSet setResult = new DataSet();
            DataTable tbLocal = null;
            DataRow dwLocal = null;
            bool flgExist = false;
            setResult = QueryXRCV(dwData);
            if (setResult.Tables.Count > 0)
            {
                tbLocal = setResult.Tables[0];
                if (tbLocal.Rows.Count > 0)
                {
                    flgExist = true;
                    dwLocal = tbLocal.Rows[0];
                }
                else
                {
                    dwLocal = tbLocal.NewRow();
                }
            }
            MapXRCV(ref dwLocal, dwData);

            if (!flgExist)
            {

                InsertXRCV(dwLocal, ITXDBVERSION);
            }
            UpdateXRCV(dwLocal);
        }
        public void InsertXRCV(DataRow dwData, string ITXDBVERSION = "50.24")
        {
            OracleCommand or1;
            or1 = OracleDB.CreateCommand();
            switch (ITXDBVERSION)
            {
                case "YGR":
                    or1.CommandText = "INSERT INTO XRCV(VAKBMANDANT, VAKBKOLL, VAKBART, VAKBKUNDNR, VAKBFARBE, VAKBKDARTNR, VAKBKDARTBEZ, VAKBKDFARBNR, VAKBANDRUCK, VAKBMEGEW, VAKBMENGEH, VAKBGEWVON, VAKBGEWBIS, VAKBGEWCA, VAKBGEWFKT, VAKBGEWGRP, VAKBMEGFKT, VAKBMEGGRP, VAKBAENDZL, VAKBAENDBEN, VAKBAENDDAT, VAKBNEUBEN, VAKBNEUDAT, VAKBFILLER)"
                        + " VALUES (:VAKBMANDANT, :VAKBKOLL, :VAKBART, :VAKBKUNDNR, :VAKBFARBE, '                         ', '                         ', '                             ', ' ', '   ', '   ', 0, 0, ' ', '000000000000', ' ', '000000000000', ' ', 0, '        ', '000000', '        ', '      ', '                                          ')";
                    or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                //VAFMANDANT VAFKOLL VAFART VAFFARBE VAFBEZ VAFFFARBE

            }
            
            Connect2Proxy(or1);
        }
        public DataSet QueryXRCV(DataRow dwData, int option = 1)
        {
            DataSet Return;
            DataSet Result = new DataSet();
            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 0:
                    or1.CommandText = "SELECT * FROM XRCV WHERE  ROWNUM<2 ";
                    break;
              
                case 99:
                    or1.CommandText = "SELECT * FROM XRCV  WHERE ROWNUM<50";
                    break;
                default:
                    break;
            }
            Result = Connect2Proxy(or1);
            if (Result.Tables.Count > 0)
                Result.Tables[0].TableName = "XRCV";
            Return = Result;
            return Return;
        }
        public void DeleteXRCV(DataRow dwData, int option = 1)
        {

            OracleCommand or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    or1.CommandText = "DELETE FROM XRCV   WHERE  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                     or1.Parameters.Add("VAKBMANDANT", OracleType.NVarChar).Value = dwData["VAKBMANDANT"].ToString();
                    or1.Parameters.Add("VAKBKOLL", OracleType.NVarChar).Value = dwData["VAKBKOLL"].ToString();
                    or1.Parameters.Add("VAKBART", OracleType.NVarChar).Value = dwData["VAKBART"].ToString();
                    or1.Parameters.Add("VAKBFARBE", OracleType.NVarChar).Value = dwData["VAKBFARBE"].ToString();
                    or1.Parameters.Add("VAKBKUNDNR", OracleType.NVarChar).Value = dwData["VAKBKUNDNR"].ToString();
                    break;
                default:
                    break;
            }
            Connect2Proxy(or1);
        } 
      
        public void UpdateXRCV(DataRow dwData, string ITXDBVERSION = "50.24", int option = 1)
        {
            OracleCommand or1;
            DataSet Result = new DataSet();
            or1 = OracleDB.CreateCommand();
            switch (option)
            {
                case 1:
                    #region====Command==== 
                    or1.CommandText = "UPDATE XRCV SET "
                    +"KBXRMANDT= :KBXRMANDT,"
                    +"KBXRWERKS= :KBXRWERKS,"
                    +"KBXRTGINR= :KBXRTGINR,"
                    +"KBXRTGMON= :KBXRTGMON,"
                    +"KBXRTGDAY= :KBXRTGDAY,"
                    +"KBXRTGLNR= :KBXRTGLNR,"
                    +"KBXRQUITT= :KBXRQUITT,"
                    +"KBXRPRTFL= :KBXRPRTFL,"
                    +"KBXRTGTIM= :KBXRTGTIM,"
                    +"KBXRSNDRP= :KBXRSNDRP,"
                    +"KBXRTNONR= :KBXRTNONR,"
                    +"KBXRSWERK= :KBXRSWERK,"
                    +"KBXRSNONR= :KBXRSNONR,"
                    +"KBXRTGFREE= :KBXRTGFREE,"
                    +"KBXRTNNAM= :KBXRTNNAM,"
                    +"KBXRSNNAM= :KBXRSNNAM,"
                    +"KBXRTAPPL= :KBXRTAPPL,"
                    +"KBXRSAPPL= :KBXRSAPPL,"
                    +"KBXRDAT01= :KBXRDAT01,"
                    +"KBXRDAT02= :KBXRDAT02,"
                    +"KBXRSTATU= :KBXRSTATU,"
                    +"KBXRQWERK= :KBXRQWERK,"
                    +"KBXRQNONR= :KBXRQNONR,"
                    +"KBXRQCONN= :KBXRQCONN,"
                    +"KBXRSTAMP= :KBXRSTAMP,"
                        + " WHERE  "
                        + "  VAKBMANDANT=:VAKBMANDANT AND VAKBKOLL=:VAKBKOLL AND VAKBART =:VAKBART AND VAKBFARBE=:VAKBFARBE AND VAKBKUNDNR=:VAKBKUNDNR";
                    #endregion

                    #region====Parameters====
          or1.Parameters.Add("KBXRMANDT", OracleType.NVarChar).Value = dwData["KBXRMANDT"].ToString(); 
          or1.Parameters.Add("KBXRWERKS", OracleType.NVarChar).Value = dwData["KBXRWERKS"].ToString(); 
          or1.Parameters.Add("KBXRTGINR", OracleType.NVarChar).Value = dwData["KBXRTGINR"].ToString(); 
          or1.Parameters.Add("KBXRTGMON", OracleType.NVarChar).Value = dwData["KBXRTGMON"].ToString(); 
          or1.Parameters.Add("KBXRTGDAY", OracleType.NVarChar).Value = dwData["KBXRTGDAY"].ToString(); 
          or1.Parameters.Add("KBXRTGLNR", OracleType.NVarChar).Value = dwData["KBXRTGLNR"].ToString(); 
          or1.Parameters.Add("KBXRQUITT", OracleType.NVarChar).Value = dwData["KBXRQUITT"].ToString(); 
          or1.Parameters.Add("KBXRPRTFL", OracleType.NVarChar).Value = dwData["KBXRPRTFL"].ToString(); 
          or1.Parameters.Add("KBXRTGTIM", OracleType.NVarChar).Value = dwData["KBXRTGTIM"].ToString(); 
          or1.Parameters.Add("KBXRSNDRP", OracleType.NVarChar).Value = dwData["KBXRSNDRP"].ToString(); 
          or1.Parameters.Add("KBXRTNONR", OracleType.NVarChar).Value = dwData["KBXRTNONR"].ToString(); 
          or1.Parameters.Add("KBXRSWERK", OracleType.NVarChar).Value = dwData["KBXRSWERK"].ToString(); 
          or1.Parameters.Add("KBXRSNONR", OracleType.NVarChar).Value = dwData["KBXRSNONR"].ToString(); 
          or1.Parameters.Add("KBXRTGFREE", OracleType.NVarChar).Value = dwData["KBXRTGFREE"].ToString(); 
          or1.Parameters.Add("KBXRTNNAM", OracleType.NVarChar).Value = dwData["KBXRTNNAM"].ToString(); 
          or1.Parameters.Add("KBXRSNNAM", OracleType.NVarChar).Value = dwData["KBXRSNNAM"].ToString(); 
          or1.Parameters.Add("KBXRTAPPL", OracleType.NVarChar).Value = dwData["KBXRTAPPL"].ToString(); 
          or1.Parameters.Add("KBXRSAPPL", OracleType.NVarChar).Value = dwData["KBXRSAPPL"].ToString(); 
          or1.Parameters.Add("KBXRDAT01", OracleType.NVarChar).Value = dwData["KBXRDAT01"].ToString(); 
          or1.Parameters.Add("KBXRDAT02", OracleType.NVarChar).Value = dwData["KBXRDAT02"].ToString(); 
          or1.Parameters.Add("KBXRSTATU", OracleType.NVarChar).Value = dwData["KBXRSTATU"].ToString(); 
          or1.Parameters.Add("KBXRQWERK", OracleType.NVarChar).Value = dwData["KBXRQWERK"].ToString(); 
          or1.Parameters.Add("KBXRQNONR", OracleType.NVarChar).Value = dwData["KBXRQNONR"].ToString(); 
          or1.Parameters.Add("KBXRQCONN", OracleType.NVarChar).Value = dwData["KBXRQCONN"].ToString(); 
          or1.Parameters.Add("KBXRSTAMP", OracleType.NVarChar).Value = dwData["KBXRSTAMP"].ToString(); 
                    #endregion
                    break; 
                default:
                    break;
            }


            Connect2Proxy(or1);
        }
    }
}
